import { Component, OnInit } from '@angular/core';
import { Location as Location1 } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { PostService } from 'src/app/service/post.service';
import { ConfirmationService, MessageService } from 'primeng/api';

@Component({
    selector: 'app-account-post-manage',
    templateUrl: './account-post-manage.component.html',
    styleUrls: ['./account-post-manage.component.scss'],
    providers: [ConfirmationService, MessageService],
})
export class AccountPostManageComponent implements OnInit {
    id: any;
    tableData: any;
    currentItem: any;
    hasChange = false;
    accountList: any;
    constructor(
        private location: Location1,
        private route: ActivatedRoute,
        private postService: PostService,
        private confirmationService: ConfirmationService,
        private messageService: MessageService
    ) {
        route.queryParams.subscribe((queryParams) => {
            this.id = queryParams.id;
            console.log(this.id);
        });
    }

    ngOnInit(): void {
        this.search();
    }
    goBack() {
        this.location.back();
    }
    async search() {
        const id = this.id;
        const r = await this.postService.fetchAccountsListById({ id });
        this.tableData = r || [];
        this.accountList = this.tableData;
        const it = await this.postService.fetchPostById({ id });
        this.currentItem = it || {};
    }
    selectAllByObj(o) {
        const r = this.tableData;
        for (let i = 0; i < r.length; i++) {
            const it = r[i];
            it.roleId = o;
            if (it.children) {
                for (let j = 0; j < it.children.length; j++) {
                    const c = it.children[j];
                    c.roleId = o;
                }
            }
        }
        this.hasChange = true;
    }
    selectAll() {
        this.selectAllByObj(this.id);
    }
    selectNone() {
        this.selectAllByObj(null);
    }
    reset = async () => {
        await this.search();
        this.hasChange = false;
    };
    selectRow(item) {
        const r = this.tableData;
        const i = r.findIndex((it) => item.id === it.id);
        if (i >= 0) {
            r[i].roleId = this.id;
            this.hasChange = true;
        }
    }
    unSelectRow(item) {
        const r = this.tableData;
        const i = r.findIndex((it) => item.id === it.id);
        if (i >= 0) {
            r[i].roleId = null;
            this.hasChange = true;
        }
    }
    async save() {
        const genSaveList = (list) => {
            return list
                .map((item) => ({
                    id: item.id + '',
                    roleId: item.roleId,
                }))
                .filter((item) => item.roleId);
        };
        const res = await this.postService.saveAccount2PostByList({
            id: this.id,
            list: genSaveList(this.tableData),
        });
        if (res.code === '200') {
            this.messageService.add({
                severity: 'info',
                summary: '确认',
                detail: '岗位名单已经修改',
            });
            this.reset();
        }
    }
}
